package com.datasecops.scannerservices.operation;


import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Closeables;
import com.google.inject.Module;
import org.jclouds.ContextBuilder;
import org.jclouds.io.Payload;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.swift.v1.SwiftApi;
import org.jclouds.openstack.swift.v1.domain.Container;
import org.jclouds.openstack.swift.v1.features.ContainerApi;
import org.jclouds.openstack.swift.v1.features.ObjectApi;
import org.jclouds.openstack.swift.v1.options.CreateContainerOptions;
import org.jclouds.openstack.swift.v1.options.PutOptions;
import java.io.IOException;
import java.util.Set;
import static com.google.common.io.ByteSource.wrap;
import static org.jclouds.io.Payloads.newByteSourcePayload;


/**
 * @author: GeXiaoLong
 * @time: 2022/11/17 15:29
 */
public class JCloudsSwift implements Cloneable {
    public static final String CONTAINER_NAME = "container1";
    public static final String OBJECT_NAME = "KingbaseES数据库参考手册.pdf";
    private SwiftApi swiftApi;

    public static void mandate() {
        JCloudsSwift jcloudsSwift = new JCloudsSwift();
        try {
            jcloudsSwift.createContainer();
            jcloudsSwift.uploadObjectFromString();
            jcloudsSwift.listContainers();
            jcloudsSwift.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                jcloudsSwift.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private void createContainer(){
        System.out.println("Create Container");

        ContainerApi containerApi = swiftApi.getContainerApi("RegionOne");
        CreateContainerOptions options = CreateContainerOptions.Builder
                .metadata(ImmutableMap.of(
                        "key1", "value1",
                        "key2", "value2"));

        containerApi.create(CONTAINER_NAME, options);

        System.out.println("  " + CONTAINER_NAME);
    }

    private void uploadObjectFromString(){
        System.out.println("Upload Object From String");

        ObjectApi objectApi = swiftApi.getObjectApi("RegionOne", CONTAINER_NAME);
        Payload payload = newByteSourcePayload(wrap("Hello World".getBytes()));

        objectApi.put(OBJECT_NAME, payload, PutOptions.Builder.metadata(ImmutableMap.of("key1", "value1")));

        System.out.println("  " + OBJECT_NAME);
    }

    public JCloudsSwift(){
        Iterable<Module> modules = ImmutableSet.<Module>of(
                new SLF4JLoggingModule());
        String provider = "openstack-swift";
        String identity = "demo:demo"; // tenantName:userName
        String credential = "openstack";

        swiftApi = ContextBuilder.newBuilder(provider)
                .endpoint("http://10.26.18.116:5000/v3.0/")
                .credentials(identity, credential)
                .modules(modules)
                .buildApi(SwiftApi.class);
    }

    private void listContainers() {
        System.out.println("List Containers");

        ContainerApi containerApi = swiftApi.getContainerApi("RegionOne");
        Set<Container> containers = containerApi.list().toSet();

        for (Container container : containers) {
            System.out.println("  " + container);
        }

    }
    public void close() throws IOException {
        Closeables.close(swiftApi, true);
    }


    public static void main(String[] args) {
        String ss = "识别\"内部文件\"关键词的策略";
        String replace = ss.replace("\"", "\\\"");
        System.out.println(replace);
        System.out.println(ss);
        // System.out.println("123456");
        mandate();
    }

}